//
//  InspectionRadioCell.swift
//  Employee
//
//  Created by swordjoy on 2017/3/29.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class InspectionRadioCell: UITableViewCell {
  
  static var identifier = "InspectionRadioCell.swordjoy"
  
  static func height(with titles: [Option]) -> CGFloat {
    var calculateY: CGFloat = 0
    for option in titles { calculateY += (UIFont.calculateHeight(width: UIScreen.width - 130, font: GlobalConfig.system14Font, text: option.name) + 30) }
    return calculateY + 100
  }
  
  var getSelect: ( (String, String, String) -> () )?
  
  var selectIndex: Int = 0 {
    didSet {
      // -1初始状态
      if -1 == selectIndex {
        
        for button in radioButtons { button.isSelected = false }
        return
      }
      
      radioButtons[selectIndex].isSelected = true
    }
  }
  
  var name: String = "" {
    didSet {
      titleLabel.sj.height = UIFont.calculateHeight(width: UIScreen.width - 60, font: GlobalConfig.system16Font, text: name)
      titleLabel.text = name
    }
  }
  
  var titles: [Option] = [] {
    didSet { hotLayoutButtons(with: titles) }
  }
  
  var isFlow: Bool = false {
    didSet {
      if isFlow {
        contentV.snp.updateConstraints({ (make) in
          make.left.right.top.bottom.equalToSuperview()
        })
      }
    }
  }
  
  fileprivate var contentV: UIView!
  
  fileprivate var radioButtons: [UIButton] = []
  
  fileprivate var helper: RadioButtonHelper!
  
  fileprivate var titleLabel: UILabel!
  
  fileprivate var margin: CGFloat = 65
  
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    contentView.isUserInteractionEnabled = true
    backgroundColor = UIColor.clear
    selectionStyle = .none
    
    create()
    
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

// MARK: - aciton

extension InspectionRadioCell {
  
  @objc fileprivate func radioAction(_ button: UIButton) {

    helper.selectIndex = button.tag
    guard let closure = getSelect else { return }
    if helper.hasSelected {
      closure("\(button.tag)", name, titles[button.tag].name)
    } else {
      closure("-1", name, "")
    }
  }
  
}

// MARK: - fileprivate

extension InspectionRadioCell {
  
  fileprivate func create() {
    
    contentV = UIView()
    contentView.addSubview(contentV)
    contentV.backgroundColor = UIColor.white
    contentV.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.top.equalTo(5)
      make.bottom.equalTo(-5)
    }
    contentV.layer.cornerRadius = 4
    contentV.clipsToBounds = true
    
    titleLabel = UILabel()
    contentV.addSubview(titleLabel)
    titleLabel.font = GlobalConfig.system16Font
    titleLabel.textColor = GlobalConfig.TextDeepColor
    titleLabel.numberOfLines = 0
    titleLabel.frame = CGRect(x: 20, y: 20, width: UIScreen.width - 60, height: 0)
  }
  
  fileprivate func hotLayoutButtons(with titles: [Option]) {
    for button in radioButtons { if let _ = button.superview { button.removeFromSuperview() } }
    radioButtons.removeAll()
    
    var cacheY: CGFloat = 0
    
    for (index, title) in titles.enumerated() {
      let height = UIFont.calculateHeight(width: UIScreen.width - 130, font: GlobalConfig.system14Font, text: title.name)
      
      let button = UIButton(frame: CGRect(x: 40, y:  cacheY + margin, width: UIScreen.width - 130, height: height + 30))
      button.setTitle(title.name, for: .normal)
      button.setTitleColor(GlobalConfig.BGSkyBlueColor, for: .selected)
      button.setTitleColor(GlobalConfig.TextDeepColor, for: .normal)
      let selectedImage = UIImage(named: "show-list-radio-icon-selected")
      button.setImage(selectedImage, for: .selected)
      let normalImage = UIImage(named: "show-list-radio-icon")
      button.setImage(normalImage, for: .normal)
      button.titleLabel?.numberOfLines = 0
      button.titleLabel?.font = GlobalConfig.system14Font
      button.addTarget(self, action: #selector(radioAction(_:)), for: .touchUpInside)
      button.contentHorizontalAlignment = .left
      button.tag = index
      
      button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 0)
  
      cacheY += (height + 30)
      
      radioButtons.append(button)
      contentV.addSubview(button)
      
    }
    
    helper = RadioButtonHelper(radioButtons: radioButtons)
  
  }
  
}
